const http = require('http')
const config = require('../config')

let limit24coinarrr=[ //市值前24币种
    {symbol:'BTC',data:null},
    {symbol:'ETH',data:null},
    {symbol:'XRP',data:null},
    {symbol:'BCH',data:null},
    {symbol:'EOS',data:null},
    {symbol:'LTC',data:null},
    {symbol:'ADA',data:null},
    {symbol:'TRX',data:null},
    {symbol:'NEO',data:null},
    {symbol:'DASH',data:null},
    {symbol:'XEM',data:null},
    {symbol:'VEN',data:null},
    {symbol:'ETC',data:null},
    {symbol:'QTUM',data:null},
    {symbol:'OMG',data:null},
    {symbol:'ZEC',data:null},
    {symbol:'ONT',data:null},
    {symbol:'ZIL',data:null},
    {symbol:'STEEM',data:null},
    {symbol:'GNT',data:null},
    {symbol:'HSR',data:null},
    {symbol:'IOST',data:null},
    {symbol:'WICC',data:null},
    {symbol:'SOC',data:null}
]

const huobicoinPrice = async () => { //获取火币24币种行情
    const result = new Promise((resolve, reject) => {
        let opt={
            hostname:config.hostname,
            port:config.port,
            path:`/market/huobi`,
            method:'GET'
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                let huobiarr=JSON.parse(resdata)
                if(huobiarr.code==200){
                    limit24coinarrr.forEach((ele,num) => {
                        for (let key in huobiarr.data) {
                            if(ele.symbol==key){
                                ele.data=huobiarr.data[key][0]
                                ele.data.extent=parseFloat(Number((huobiarr.data[key][0].high-huobiarr.data[key][0].open)/huobiarr.data[key][0].open*100).toFixed(4))
                            }
                        }
                    })
                    resolve(limit24coinarrr)
                }else{
                    resolve([])
                }
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    return result
}
 
const huobicoinKline = async (ctx,next) => { //获取火币24币种行情
    const result = new Promise((resolve, reject) => {
        let opt={
            hostname:config.hostname,
            port:config.port,
            path:`/market/huobi`,
            method:'GET'
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                let huobiarr=JSON.parse(resdata)
                let higharr = []
                if(huobiarr.code==200){
                    limit24coinarrr.forEach((ele,num) => {
                        for (let key in huobiarr.data) {
                            if(ele.symbol==key){
                                ele.data=huobiarr.data[key]
                                ele.data.extent=parseFloat(Number((huobiarr.data[key][0].high-huobiarr.data[key][0].open)/huobiarr.data[key][0].open*100).toFixed(4))
                            }
                        }
                    })
                    resolve(limit24coinarrr)
                }else{
                    resolve([])
                }
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    // return result
    let data=await result
    if(data.length){
        let responsedata={
            code:200,
            data:data
        }
        ctx.success(responsedata)
    }else{
        let responsedata={
            code:200,
            data:null
        }
        ctx.error(responsedata)
    }
}

module.exports = {
    huobicoinPrice,
    huobicoinKline
}